package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.pojo.*;
import com.gxa.zhonglong.service.InventoryOutService;
import com.gxa.zhonglong.service.IoGetService;
import com.gxa.zhonglong.service.StorageService;
import com.gxa.zhonglong.util.CodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Api("配货出库接口")
@CrossOrigin
@RestController
@RequestMapping("/inventoryout")
public class InventoryOutController {

    @Autowired
    InventoryOutService inventoryOutService ;
    @Autowired
    StorageService storageService;
    @Autowired
    IoGetService ioGetService;
    CodeUtil codeUtil = new CodeUtil();

    @ApiOperation("添加采购退货的方法")
    @PostMapping("/add")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name="inventoryOut",value = "添加的采购退货",required = true,paramType = "query"),*/
            @ApiImplicitParam(name= "map",value = "商品编码的集合",required = true,paramType = "body"),

    })
    public Map add(/*@RequestParam */InventoryOut inventoryOut, @RequestBody Map map){
        List<String> numberList = (List) map.get("ids");
        System.out.println(inventoryOut);
        inventoryOut.setInventoryOutNumber(codeUtil.XSCKDencode());
        //inventoryOut.setInventoryOutCtime(ctime);
        //inventoryOut.setInventoryOutCusername(cname);
        inventoryOutService.add(inventoryOut);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        for(int i=0;i<numberList.size();i++){
            IoGet ioGet = new IoGet();
            ioGet.setIoGetId(inventoryOut.getInventoryOutNumber());
            ioGet.setIoGetGid(numberList.get(i));
            ioGet.setIoGetAmount(storageService.selectByCount(numberList.get(i)));
            ioGet.setIoGetWeight(storageService.selectByWeight(numberList.get(i)));
            ioGet.setIoGetCost(storageService.selectByCost(numberList.get(i)));
            ioGetService.add(ioGet);
        }
        return jsonResult.getDataMap();
    }

    @PostMapping("/getListByID")
    @ApiOperation(value = "通过多个商品编码来批量获取商品信息的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",value = "商品编码的集合",required = true,paramType  = "body")
    })
    public Map getListByID(@RequestBody  Map<String,List> map ){
        List list = (List)map.get("ids");
        System.out.println(list);
        List<Storage> listByID = inventoryOutService.getListByID(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(listByID);
        return jsonResult.getDataMap();
    }
    @PostMapping("/selectByNum")
    @ApiOperation("通过单据编号获得采购退货单信息的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number",value = "单据编号",paramType = "String")
    })
    public  Map selectByNum(String  number){
        InventoryOut inventoryOut = inventoryOutService.selectByNum(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(inventoryOut);
        return  jsonResult.getDataMap();
    }
    @GetMapping("/reject")
    @ApiOperation("审批不通过的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num",value = "单据编号",paramType = "String"),
            @ApiImplicitParam(name="ename",value = "审批人名字",paramType = "String"),
            @ApiImplicitParam(name="etime",value = "审批时间",paramType = "Timestamp")
    })
    public Map reject(String ename, Timestamp etime,String num){
        inventoryOutService.reject(ename,etime,num);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("审批成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/revokereject")
    @ApiOperation("撤销审批驳回的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number",value = "单据编号",paramType = "String")
    })
    public Map revokeReject(String number){
        inventoryOutService.revokeReject(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("撤销驳回成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/approve")
    @ApiOperation("审批通过的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num",value = "单据编号",paramType = "String"),
            @ApiImplicitParam(name="ename",value = "审批人名字",paramType = "String"),
            @ApiImplicitParam(name="etime",value = "审批时间",paramType = "Timestamp")
    })
    public Map approve(String ename, Timestamp etime,String num){
        List<String> list = ioGetService.selectByNum(num);
        InventoryOut inventoryOut = inventoryOutService.selectByNum(num);
        storageService.updateSAC(inventoryOut.getInventoryOutInshop(),inventoryOut.getInventoryOutIncounter(),num);
        inventoryOutService.approve(ename,etime,num);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("审批成功");
        return jsonResult.getDataMap();
    }
    @PostMapping("/delete")
    @ApiOperation("删除单个订单中的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="num",value = "商品编码",paramType = "String")
    })
    public Map delet(String num){
        ioGetService.delete(num);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return  jsonResult.getDataMap();

    }





}
